package com.colodoo.framework.creater.application;

import com.colodoo.framework.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.SqlMapGeneratorConfiguration;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Java应用
 * 
 * @author colodoo
 *
 */
@Slf4j
public class Generator {

	public static void main(String[] args) {
		Map values = new HashMap();
		// 项目根目录
		values.put("projectPath", "E:\\dev\\project\\lanb-manager\\lanb-parent\\lanb-framwwork");
		// 模块包名
		values.put("targetPackage", "com.colodoo.framework.manager.test");
		// 模块表名
		values.put("tableName", "test");
		Generator.runMybatis(values);
	}

	public static void runMybatis(Map<String, String> values) {
		try {
			String targetPackage = values.get("targetPackage");
			String tableName = values.get("tableName");
			String projectPath = values.get("projectPath");
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			String srcPath = new File("lanb-framework").getAbsolutePath();
			log.info("当前项目根目录: " + srcPath);
			String configFilePath = srcPath + "\\src\\main\\resources\\generatorConfig.xml";
			File configFile = new File(configFilePath);
			ConfigurationParser cp = new ConfigurationParser(warnings);
			org.mybatis.generator.config.Configuration config = cp.parseConfiguration(configFile);
			for (Context ctx : config.getContexts()) {
				// 设置model生成位置
				JavaModelGeneratorConfiguration model = ctx.getJavaModelGeneratorConfiguration();
				model.setTargetPackage(targetPackage + ".model");
				model.setTargetProject("./src/main/java");
				model.getProperties().setProperty("enableSubPackages", "true");
				model.getProperties().setProperty("trimStrings", "true");
				// 设置xml文件生成位置
				SqlMapGeneratorConfiguration sqlMapper = ctx.getSqlMapGeneratorConfiguration();
				sqlMapper.setTargetPackage(targetPackage + ".service");
				sqlMapper.setTargetProject("./src/main/java");
				sqlMapper.getProperties().setProperty("enableSubPackages", "true");
				// 设置mapper接口文件生成位置
				JavaClientGeneratorConfiguration javaMapper = ctx.getJavaClientGeneratorConfiguration();
				javaMapper.setTargetPackage(targetPackage + ".service");
				javaMapper.setTargetProject("./src/main/java");
				javaMapper.getProperties().setProperty("enableSubPackages", "true");
				// 配置表信息
				ctx.getTableConfigurations().clear();
				TableConfiguration tableCfg = new TableConfiguration(ctx);
				tableCfg.setTableName(tableName);
				StringUtil.upperCase(StringUtil.underlineToCamel2(tableName));
				ctx.getTableConfigurations().add(tableCfg);
			}
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
